/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.star.framework.core.api.Pageable;
import cn.star.framework.strategy.UpdateStrategy;
import cn.star.framework.support.QueryBuilder;
import java.io.Serializable;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

/**
 * IService<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public interface IService<T extends IEntity, Id extends Serializable> {
  /**
   * 新增/更新数据
   *
   * @param entity {@link S}
   * @return {@link S}
   */
  default <S extends T> S save(S entity) {
    return this.save(entity, UpdateStrategy.NORMAL);
  }

  /**
   * 新增/更新数据
   *
   * @param entity {@link S}
   * @param flush 刷新
   * @return {@link S}
   */
  default <S extends T> S save(S entity, boolean flush) {
    return this.save(entity, UpdateStrategy.NORMAL, flush);
  }

  /**
   * 新增/更新数据
   *
   * @param entities {@link S}
   * @return {@link S}
   */
  default <S extends T> List<S> save(S... entities) {
    return this.save(CollectionUtil.toList(entities));
  }

  /**
   * 新增/更新数据
   *
   * @param entities {@link S}
   * @return {@link S}
   */
  default <S extends T> List<S> save(Iterable<S> entities) {
    return this.save(entities, UpdateStrategy.NORMAL);
  }

  /**
   * 新增/更新数据
   *
   * @param entity {@link S}
   * @param strategy {@link UpdateStrategy}
   * @return {@link S}
   */
  <S extends T> S save(S entity, UpdateStrategy strategy);

  /**
   * 新增/更新数据
   *
   * @param entity {@link S}
   * @param strategy {@link UpdateStrategy}
   * @param flush 刷新
   * @return {@link S}
   */
  <S extends T> S save(S entity, UpdateStrategy strategy, boolean flush);

  /**
   * 新增/更新数据
   *
   * @param entities {@link S}
   * @param strategy {@link UpdateStrategy}
   * @return {@link S}
   */
  <S extends T> List<S> save(Iterable<S> entities, UpdateStrategy strategy);

  /**
   * 删除数据
   *
   * @param ids {@link Id}
   * @return 数据集合
   */
  default List<T> deleteByIds(Id... ids) {
    return this.deleteByIds(CollectionUtil.toList(ids));
  }

  /**
   * 删除数据
   *
   * @param ids {@link Id}
   * @return 数据集合
   */
  List<T> deleteByIds(Iterable<Id> ids);

  /**
   * 删除数据
   *
   * @param entities {@link T}
   */
  default void delete(T... entities) {
    this.delete(CollectionUtil.toList(entities));
  }

  /**
   * 删除数据
   *
   * @param entities {@link T}
   */
  void delete(Iterable<T> entities);

  /**
   * 查询数据
   *
   * @return 数据集合
   */
  List<T> findAll();

  /**
   * 查询数据
   *
   * @param spec 查询条件{@link Specification}
   * @return 数据集合
   */
  List<T> findAll(Specification<T> spec);

  /**
   * 查询数据
   *
   * @param spec 查询条件{@link Specification}
   * @param sort 排序参数{@link Sort}
   * @return 数据集合
   */
  List<T> findAll(Specification<T> spec, Sort sort);

  /**
   * 查询数据
   *
   * @param spec 查询条件{@link Specification}，支持Null
   * @return 数据条数
   */
  long count(Specification<T> spec);

  /**
   * 查询数据
   *
   * @param spec 查询条件{@link Specification}
   * @return 存在返回True
   */
  boolean isExists(Specification<T> spec);

  /**
   * 查询数据
   *
   * @param id 主键{@link Id}
   * @return 存在返回True
   */
  boolean isExists(Id id);

  /**
   * 查询数据
   *
   * @param spec 查询条件{@link Specification}
   * @return 一条数据
   */
  T findOne(Specification<T> spec);

  /**
   * 查询数据
   *
   * @param id {@link Id}
   * @return 数据
   */
  T findById(Id id);

  /**
   * 查询数据<br>
   * 支持联合主键查询
   *
   * @param entity {@link T}
   * @return 数据
   * @throws Exception 使用主键列表查询实例不唯一时抛出异常
   */
  T findById(T entity) throws Exception;

  /**
   * 查询数据
   *
   * @param ids {@link Id}
   * @return 数据集合
   */
  default List<T> findAllByIds(Id... ids) {
    return this.findAllByIds(CollectionUtil.toList(ids));
  }

  /**
   * 查询数据
   *
   * @param ids {@link Id}
   * @return 数据集合
   */
  List<T> findAllByIds(Iterable<Id> ids);

  /**
   * 查询数据(分页)
   *
   * @param spec 查询条件{@link Specification}
   * @param pageNo 页码
   * @param pageSize 页数据
   * @return 分页数据
   */
  Pageable<T> pageable(Specification<T> spec, int pageNo, int pageSize);

  /**
   * 查询数据(分页)
   *
   * @param pageNo 页码
   * @param pageSize 页数据
   * @return 分页数据
   */
  default Pageable<T> pageable(int pageNo, int pageSize) {
    return this.pageable((Specification<T>) (root, query, cb) -> cb.and(), 1, 20);
  }

  /**
   * 查询数据<br>
   *
   * @param spec 查询条件{@link Specification}
   * @param pageNo 页码
   * @param pageSize 页数据
   * @param sort 分页参数(排序){@link Sort}
   * @return 数据集合
   */
  Pageable<T> pageable(Specification<T> spec, int pageNo, int pageSize, Sort sort);

  /**
   * 查询数据
   *
   * @param builder 查询条件{@link QueryBuilder}
   * @return 数据集合
   */
  List<T> findAll(QueryBuilder builder);

  /**
   * 查询数据(分页)
   *
   * @param builder 查询条件{@link QueryBuilder}
   * @param pageNo 页码
   * @param pageSize 页数据
   * @return 分页数据
   */
  Pageable<T> pageable(QueryBuilder builder, int pageNo, int pageSize);
}
